package com.ls.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.FeatureDescriptor;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Random;

public class PyBeanUtil {

    /**
     * 低频和单机模式下，直接用synchronized来处理
     * */
    public static synchronized String getMerchantCode() throws InterruptedException {
        Random random = new Random();
        int millSeconds = random.nextInt(200);
//        long millSeconds = (long)(Math.random()*200);
        Thread.sleep(millSeconds);
        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String str = ldt.format(dtf);
        return str;
    }

    public static <T> T copyPropertiesIgnoreNull(Object src, T dst) {
        final BeanWrapper beanWrapper = new BeanWrapperImpl(src);
        BeanUtils.copyProperties(src, dst, Arrays
                .stream(beanWrapper.getPropertyDescriptors())
                .map(FeatureDescriptor::getName)
                .filter(name -> beanWrapper.getPropertyValue(name) == null)
                .toArray(String[]::new)
        );
        return dst;
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.printf(PyBeanUtil.getMerchantCode());
    }
}
